package com.kvest.ula.datamodel;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Contact 
{
	public static final int PHONE = 1;
    public static final int EMAIL = 2;
    public static final int SITE = 3;
    public static final int ICQ = 4;

    private String contact;
    private int type;

    public Contact(String contact, int type)
    {
        this.contact = contact;
        this.type = type;
    }

    public Contact(byte[] data) throws IOException
    {
    	ByteArrayInputStream bais = new ByteArrayInputStream(data);
    	DataInputStream dis = new DataInputStream(bais);
    	try
        {
    		//type
    		type = dis.readInt();
    		
    		//contact
    		contact = dis.readUTF();
        }
    	finally
        {
            try
            {
                dis.close();
                bais.close();
            }catch(Exception ex){}
        }
    }

    public String getContact()
    {
        return contact;
    }

    public int getType()
    {
        return type;
    }

    public byte[] getBytes()
    {
    	ByteArrayOutputStream result = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(result);
        
        try
        {
        	//type
    		dos.writeInt(type);
    		
    		//contact
    		dos.writeUTF(contact);
    		
        	return result.toByteArray();
        }
        catch(IOException ioex)
        {
            return (new byte[]{});
        }
        finally
        {
            try
            {
                dos.close();
                result.close();
            }catch(Exception ex){}
        }
    }

}
